﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class InstructorMainScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindInstructorInfo(  );
                BindCourseInfo( );
            }
        }

        private void BindInstructorInfo()
        {
            Instructor _instructor = (Instructor) EntityFactory.User;

            imgInstructor.ImageUrl = _instructor.PhotoFile;
            lblUserName.Text = _instructor.UserName;
            lblNameSurname.Text = _instructor.Name + ' ' + _instructor.Surname;
            lblEMail.Text = _instructor.Email;

        }

        protected void OnLnkBtnEditProfileClicked(object sender, EventArgs e)
        {
            Response.Redirect( "~/EditProfileScreen.aspx" );
        }

        private void BindCourseInfo()
        {
            Instructor _instructor = (Instructor) EntityFactory.User;
            dtgCourses.DataSource = EntityFactory.GetCourseOfInstructor( _instructor.UserId );
            dtgCourses.DataBind();
        }
            
        protected void OnLnkBtnCourseNameClicked(object sender, EventArgs e)
        {
            int _courseId =
                CommonMethods.GetIdFromGridViewRow((GridViewRow)(((LinkButton)sender).NamingContainer));
            EntityFactory.CourseId = _courseId;
            CourseInstructor _course = new CourseInstructor( _courseId );
            _course.GoToCourseMainPage( Response  );
        }

        protected void OnLnkBtnDeleteClicked(object sender, EventArgs e)
        {
            Instructor _instructor = (Instructor)EntityFactory.User;
            int _courseId =
                CommonMethods.GetIdFromGridViewRow( ( GridViewRow ) ( ( ( LinkButton ) sender ).NamingContainer ) );
            CourseInstructor _course = new CourseInstructor( _courseId );
            _instructor.DeleteCourse(_course);

            BindCourseInfo(  );
        }
            
        protected void OnLnkBtnAddNewCourseClicked(object sender, EventArgs e)
        {
            Response.Redirect( "~/CreateCourseScreen.aspx" );
        }

        protected void OnLnkBtnAddNewUserClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/AddNewUserScreen.aspx");
        }
    }
}